<template>
	<div class="system-edit-role-container">
		<el-dialog :title="(Number(formData.id) === 0 ? '添加' : '修改') + '银行卡绑定'" v-model="isShowDialog" width="769px">
			<el-form ref="formRef" :model="formData" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="用户ID" prop="id">
							<el-input v-model="formData.id" placeholder="请输入用户ID" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="所属银行" prop="bankAddress">
							<el-input v-model="formData.bankAddress" placeholder="请输入所属银行" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="银行卡号" prop="cardNo">
							<el-input v-model="formData.cardNo" placeholder="请输入银行卡号" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="开户行" prop="bankType">
							<el-input v-model="formData.bankType" placeholder="请输入开户行" clearable></el-input>
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="持卡姓名" prop="ownerName">
							<el-input v-model="formData.ownerName" placeholder="请输入持卡人姓名" clearable></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default" :loading="loading">{{ formData.id === 0 ? '新 增' : '修 改' }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { reactive, toRefs, defineComponent, ref, getCurrentInstance, unref } from 'vue';
import { editRole, getRole, getRoleParams } from '/@/api/system/role';
import { ElMessage } from 'element-plus';
import { refreshBackEndControlRoutes } from '/@/router/backEnd';
import { apiaddBankCardList } from '/@/api/MemberManagement/apiMemberBankcardbinding';

// 定义接口来定义对象的类型
interface MenuDataTree {
	id: number;
	pid: number;
	title: string;
	children?: MenuDataTree[];
}
interface DialogRow {
	id: string;
	cardNo: string;
	bankType: string;
	ownerName: string;
	bankAddress: string;
}
interface RoleState {
	loading: boolean;
	isShowDialog: boolean;
	formData: DialogRow;
	menuData: Array<MenuDataTree>;
	menuExpand: boolean;
	menuNodeAll: boolean;
	menuCheckStrictly: boolean;
	menuProps: {
		children: string;
		label: string;
	};
	rules: object;
}

export default defineComponent({
	name: 'systemEditRole',
	setup(props, { emit }) {
		const { proxy } = getCurrentInstance() as any;
		const formRef = ref<HTMLElement | null>(null);
		const menuRef = ref();
		const state = reactive<RoleState>({
			loading: false,
			isShowDialog: false,
			formData: {
				id: '',
				cardNo: '',
				bankType: '',
				ownerName: '',
				bankAddress: '',
			},
			// 表单校验
			rules: {
				ownerName: '',
			},
			menuData: [],
			menuExpand: false,
			menuNodeAll: false,
			menuCheckStrictly: false,
			menuProps: {
				children: 'children',
				label: 'title',
			},
		});
		// 打开弹窗 
		const openDialog = (row?: DialogRow) => {
			resetForm();
			getMenuData();
			if (row) {
				getRole(Number(row.id)).then((res: any) => {
					if (res.data.role) {
						state.formData = res.data.role;
						// state.formData.menuIds = res.data.menuIds??[]
					}
				});
			}
			state.isShowDialog = true;
		};
		// 关闭弹窗
		const closeDialog = () => {
			state.isShowDialog = false;
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};
		// 新增
		const onSubmit = () => {
			const formWrap = unref(formRef) as any;
			if (!formWrap) return;
			formWrap.validate((valid: boolean) => {
				if (valid) {
					state.loading = true;
					// state.formData.menuIds = getMenuAllCheckedKeys();
					if (Number(state.formData.id) === 0) {
						//添加
						apiaddBankCardList(state.formData)
							.then(() => {
								ElMessage.success('银行卡添加成功');
								closeDialog(); // 关闭弹窗
								resetMenuSession();
								emit('getRoleList');
							})
							.finally(() => {
								state.loading = false;
							});
					} else {
						//修改
						editRole(state.formData)
							.then(() => {
								ElMessage.success('角色修改成功');
								closeDialog(); // 关闭弹窗
								resetMenuSession();
								emit('getRoleList');
							})
							.finally(() => {
								state.loading = false;
							});
					}
				}
			});
		};
		// 获取菜单结构数据
		const getMenuData = () => {
			getRoleParams().then((res: any) => {
				state.menuData = proxy.handleTree(res.data.menu, 'id', 'pid');
			});
		};
		const resetForm = () => {
			state.menuCheckStrictly = false;
			state.menuExpand = false;
			state.menuNodeAll = false;
			state.formData = {
				// id:0,
				// name: '',
				// status: 1,
				// listOrder: 0,
				// remark: '',
				// menuIds:[]
				id: '',
				cardNo: '',
				bankType: '',
			
				ownerName: '',
				bankAddress: '',

			};
		};
		/** 树权限（展开/折叠）*/
		const handleCheckedTreeExpand = (value: any) => {
			let treeList = state.menuData;
			for (let i = 0; i < treeList.length; i++) {
				menuRef.value.store.nodesMap[treeList[i].id].expanded = value;
			}
		};

		/** 树权限（全选/全不选） */
		const handleCheckedTreeNodeAll = (value: any) => {
			menuRef.value.setCheckedNodes(value ? state.menuData : []);
		};

		/** 树权限（父子联动） */
		const handleCheckedTreeConnect = (value: any) => {
			state.menuCheckStrictly = value ? true : false;
		};

		/** 所有菜单节点数据 */
		function getMenuAllCheckedKeys() {
			// 目前被选中的菜单节点
			let checkedKeys = menuRef.value.getCheckedKeys();
			// 半选中的菜单节点
			let halfCheckedKeys = menuRef.value.getHalfCheckedKeys();
			checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
			return checkedKeys;
		}

		// 重置菜单session
		const resetMenuSession = () => {
			refreshBackEndControlRoutes();
		};
		return {
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			menuRef,
			formRef,
			handleCheckedTreeExpand,
			handleCheckedTreeNodeAll,
			handleCheckedTreeConnect,
			resetMenuSession,
			...toRefs(state),
		};
	},
});
</script>

<style scoped lang="scss">
.tree-border {
	margin-top: 5px;
	border: 1px solid #e5e6e7 !important;
	border-radius: 4px;
}
.system-edit-role-container {
	.menu-data-tree {
		border: var(--el-input-border, var(--el-border-base));
		border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
		padding: 5px;
	}
}
</style>
 